package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.XswsMapper;
import com.oracle.pojo.Xsws;
import com.oracle.pojo.XswsExample;
import com.oracle.service.XswsService;
import com.oracle.vo.XswsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class XswsServiceImpl implements XswsService {
    @Autowired(required = false)
    private XswsMapper xswsMapper;

    @Override
    public List<Xsws> findList()
    {
        List<Xsws> xsws = xswsMapper.selectByExample(null);
        return xsws;
    }

    @Override
    public PageResult findPages(Integer pageNo, Integer pageSize, Xsws xsws)
    {
        PageHelper.startPage(pageNo,pageSize);

        XswsExample example = new XswsExample();
        XswsExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(xsws.getXswsId()))
        {
            criteria.andXswsIdEqualTo(xsws.getXswsId());
        }
        if (!ObjectUtils.isEmpty(xsws.getXsStudentid()))
        {
            criteria.andXsStudentidEqualTo(xsws.getXsStudentid());
        }
        if (!ObjectUtils.isEmpty(xsws.getXsName()))
        {
            criteria.andXsNameEqualTo(xsws.getXsName());
        }
        if (!ObjectUtils.isEmpty(xsws.getXswsPf()))
        {
            criteria.andXswsPfEqualTo(xsws.getXswsPf());
        }
        if (!ObjectUtils.isEmpty(xsws.getXsZyid()))
        {
            criteria.andXsZyidEqualTo(xsws.getXsZyid());
        }
        if (!ObjectUtils.isEmpty(xsws.getXsDormitoryid()))
        {
            criteria.andXsDormitoryidEqualTo(xsws.getXsDormitoryid());
        }
        if (!ObjectUtils.isEmpty(xsws.getCjTime()))
        {
            criteria.andCjTimeEqualTo(xsws.getCjTime());
        }
        if (!ObjectUtils.isEmpty(xsws.getUpTime()))
        {
            criteria.andUpTimeEqualTo(xsws.getUpTime());
        }

        example.setOrderByClause("gly_username");

        Page<Xsws> xswsPage = (Page<Xsws>)xswsMapper.selectByExample(example);

        List<Xsws> xswsList = xswsPage.getResult();

        List<XswsVo> xswsVoList = buildGlyVoList(xswsList);

        return new PageResult(xswsPage.getTotal(),xswsVoList);
    }

    private List<XswsVo> buildGlyVoList(List<Xsws> xswsList)
    {
        List<XswsVo> xswsVoList = new ArrayList<>();
        xswsList.forEach(xsws -> {
            XswsVo xswsVo2 = new XswsVo();

            BeanUtils.copyProperties(xsws,xswsVo2);

            xswsVoList.add(xswsVo2);
        });

        return xswsVoList;
    }
}
